package com.hui.interceptor;

import com.hui.auth.dto.JsonResult;
import com.hui.auth.exception.CustomException;
import com.hui.exception.GlobalError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理
 * Created by Liaoyunhui on 2017/4/8.
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

//    @ExceptionHandler(RuntimeException.class)
//    @ResponseBody
//    public JsonResult<String> exceptionHandler(RuntimeException e) {
//        JsonResult<String> jsonResult = new JsonResult<>();
//        jsonResult.setMsg(GlobalError.SERVER_EXCEPTION.msg);
//        logger.debug("exceptionHandler {}", e);
//        return jsonResult;
//    }

    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public JsonResult<String> exceptionHandler(CustomException e) {
        JsonResult<String> jsonResult = new JsonResult<>();
        jsonResult.setCode(GlobalError.SERVER_EXCEPTION.code);
        jsonResult.setMsg(GlobalError.SERVER_EXCEPTION.msg);
        logger.error("exceptionHandler {}", e);
        return jsonResult;
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public JsonResult<String> exceptionHandler(IllegalArgumentException e) {
        JsonResult<String> jsonResult = new JsonResult<>();
        jsonResult.setCode(GlobalError.ILLEGAL_ARGUMENT_EXCEPTION.code);
        jsonResult.setMsg(GlobalError.ILLEGAL_ARGUMENT_EXCEPTION.msg);
        logger.error("exceptionHandler {}", e);
        return jsonResult;
    }
}
